/**
 * Por Josu� Hernandez
 * @class Extmx.grid.GridPanel
 * Grid con preconfiguraciones y funciones cortas a su store y modelo de seleccion
 *        Historial de revisiones<br>
 *        ---------------------------------------------<br>
 *        <b>8 de noviembre</b> | Creaci�n de la clase. 
 * 
 * @extends Ext.grid.GridPanel
 * @autor Ing. Josu� Hern�ndez G�mez<br>
 *        joshua@extjs.mx<br>
 *        http://www.joshua.com.mx
 * @fecha 8 de noviembre. Mexico DF
 * 
 */
Ext.ns("Extmx.grid.GridPanel");
Extmx.grid.GridPanel=Ext.extend(Ext.grid.GridPanel,{
    frame:false,
    border:false,
    loadMask: true,
    stripeRows:true,
	loadOnRender:true,
	viewConfig: {
		emptyText :"No hay resultados de b&uacute;squeda"
	},
    initComponent: function (){
        Ext.apply(this,{
            bbar: this._getBbar()
        });

        
        Extmx.grid.GridPanel.superclass.initComponent.apply(this,arguments);
        /*Ext.apply(this.viewConfig,{
        	
        });*/
        
		this.relayEvents(this.store,[
			/**
			 * @event load
			 * Se copia el evento load de su store. DIsparado al cargar los datos
			 * @param {Store} store el store de este grid
			 * @param {records} Arrrecords Arreglo de records cargados en el store
			 */
		     "load",
		     /**
			 * @event beforeload
			 * Disparado justo antes de cargar. Si regresa falso, el store no se carga
			 * @param {Store} store El store de este grid
			 * @param {Params} params Los parametos a enviar por post para cargar este store
			 */
		     "beforeload"]);
        
		if(this.loadOnRender)
			this.on({
				scope:this,
				render: function(){
					this.load();
				}
	        });
    },
	
	_getBbar: function (){
        return new Ext.PagingToolbar({
                store: this.store,
                displayInfo: true,
                pageSize: Extmx.NUM_PAGINAS,
                displayMsg: 'Desplegando datos {0} - {1} de {2}',
                emptyMsg: "No hay datos que mostrar"
            });
    },
	
    reload: function (d){
        this.store.reload(d);
    },
    /**
     * Add Records to the Store and fires the {@link #add} event.  To add Records
     * to the store from a remote source use {@link #load}({add:true}).
     * See also {@link #recordType} and {@link #insert}.
     * @param {Ext.data.Record[]} records An Array of Ext.data.Record objects
     * to add to the cache. See {@link #recordType}.
     */
    add : function(records){
        this.store.add(records);
    },
    /**
     * Remove a Record from the Store and fires the {@link #remove} event.
     * @param {Ext.data.Record} record The Ext.data.Record object to remove from the cache.
     */
    remove : function(record){
        this.store.remove(record);
    },


/**
     * Remove a Record from the Store at the specified index. Fires the {@link #remove} event.
     * @param {Number} index The index of the record to remove.
     */
    removeAt : function(index){
    	this.store.removeAt(index);
    },


/**
     * Remove all Records from the Store and fires the {@link #clear} event.
     */
    removeAll : function(){
    	this.store.removeAll();
    },


/**
     * Inserts Records into the Store at the given index and fires the {@link #add} event.
     * See also {@link #add} and {@link #addSorted}.
     * @param {Number} index The start index at which to insert the passed Records.
     * @param {Ext.data.Record[]} records An Array of Ext.data.Record objects to add to the cache.
     */
    insert : function(index, records){
    
    },


/**
     * Get the index within the cache of the passed Record.
     * @param {Ext.data.Record} record The Ext.data.Record object to find.
     * @return {Number} The index of the passed Record. Returns -1 if not found.
     */
    indexOf : function(record){
    
    },


/**
     * Get the index within the cache of the Record with the passed id.
     * @param {String} id The id of the Record to find.
     * @return {Number} The index of the Record. Returns -1 if not found.
     */
    indexOfId : function(id){
    
    },


    /**
     * Get the Record with the specified id.
     * @param {String} id The id of the Record to find.
     * @return {Ext.data.Record} The Record with the passed id. Returns undefined if not found.
     */
    getById : function(id){
    
    },

    /**     
     * Gets the number of cached records.     
     * If using paging, this may not be the total size of the dataset. If the data object     
     * used by the Reader contains the dataset size, then the {@link #getTotalCount} function returns     
     * the dataset size.  Note: see the Important note in {@link #load}.     
     * @return {Number} The number of Records in the Store's cache.     
     **/    
    getCount : function(){        
    	return this.store.getCount();    
    },

	/**
     * Get the Record at the specified index.
     * @param {Number} index The index of the Record to find.
     * @return {Ext.data.Record} The Record at the passed index. Returns undefined if not found.
     */
    getAt : function(index){
    	return this.store.getAt(index);
    },

/**
     *

Loads the Record cache from the configured {@link #proxy} using the configured {@link #reader}.

     *

Notes:


    *      * Important: loading is asynchronous! This call will return before the new data has been
           * loaded. To perform any post-processing where information from the load call is required, specify
           * the callback function to be called, or use a {@link Ext.util.Observable#listeners a 'load' event handler}.

    *      * If using {@link Ext.PagingToolbar remote paging}, the first load call must specify the start and limit
           * properties in the options.params property to establish the initial position within the
           * dataset, and the number of Records to cache on each read from the Proxy.

    *      * If using {@link #remoteSort remote sorting}, the configured {@link #sortInfo}
           * will be automatically included with the posted parameters according to the specified
           * {@link #paramNames}.

           *


     * @param {Object} options An object containing properties which control loading options:


    *      * params :Object

      An object containing properties to pass as HTTP
           * parameters to a remote data source. Note: params will override any
           * {@link #baseParams} of the same name.

           *

      Parameters are encoded as standard HTTP parameters using {@link Ext#urlEncode}.

    *      * callback : Function

      A function to be called after the Records
           * have been loaded. The callback is called after the load event and is passed the following arguments:

          o      * r : Ext.data.Record[]

          o      * options: Options object from the load call

          o      * success: Boolean success indicator


    *      * scope : Object

      Scope with which to call the callback (defaults
           * to the Store object)

    *      * add : Boolean

      Indicator to append loaded records rather than
           * replace the current cache.  Note: see note for {@link #loadData}

           *


     * @return {Boolean} If the developer provided {@link #beforeload} event handler returns
     * false, the load call will abort and will return false; otherwise will return true.
     */
    load : function(options) {
        this.store.load(options);
    },
    
    /**     
     * Loads data from a passed data block and fires the {@link #load} event. A {@link Ext.data.Reader Reader}     
     * which understands the format of the data must have been configured in the constructor.     
     * @param {Object} data The data block from which to read the Records.  The format of the data expected     
     * is dependent on the type of {@link Ext.data.Reader Reader} that is configured and should correspond to     
     * that {@link Ext.data.Reader Reader}'s {@link Ext.data.Reader#readRecords} parameter.     
     * @param {Boolean} append (Optional) true to append the new Records rather the default to replace     
     * the existing cache.     
     * 
     * Note: that Records in a Store are keyed by their {@link Ext.data.Record#id id}, so added Records     
     * with ids which are already present in the Store will replace existing Records. Only Records with     
     * new, unique ids will be added.     
     * */    
    loadData : function(o, append){
    	this.store.loadData(o, append);
    },

    getSeleccionado: function (){
        return this.getSelectionModel().getSelected() ;
    },
    getSeleccionados: function (){
        return this.getSelectionModel().getSelections() ;
    },
    
    /**     
     *  Selects the first row in the grid.     
     **/    
    selectFirstRow : function(){
    	this.selModel.selectFirstRow();    
    },    
    
    /**     
     * Select the last row.     
     * @param {Boolean} keepExisting (optional) true to keep existing selections     
     * */    
    selectLastRow : function(keepExisting){ 
    	this.selModel.selectLastRow(keepExisting);
    },    
    
    /**     
     * Selects the row immediately following the last selected row.     
     * @param {Boolean} keepExisting (optional) true to keep existing selections     
     * @return {Boolean} true if there is a next row, else false     
     **/    
    selectNext : function(keepExisting){  
    	return this.selModel.selectNext(keepExisting);
    },    
    
    /**     
     * Selects the row that precedes the last selected row.     
     * @param {Boolean} keepExisting (optional) true to keep existing selections     
     * @return {Boolean} true if there is a previous row, else false     
     **/    
    selectPrevious : function(keepExisting){  
    	return this.selModel.selectPrevious(keepExisting);
    },    
    
    /**     
     * Returns true if there is a next record to select     
     * @return {Boolean}     
     * */    
    hasNext : function(){   
    	return this.selModel.hasNext();
    },    
    
    /**     
     * 
     * Returns true if there is a previous record to select     
     * @return {Boolean}     
     **/    
    hasPrevious : function(){   
    	return this.selModel.hasPrevious();    
    },
    
    /**     
     * Selects a row.  
     * Before selecting a row, checks if the selection model     
     * {@link Ext.grid.AbstractSelectionModel#isLocked is locked} and fires the     
     * {@link #beforerowselect} event.  If these checks are satisfied the row     
     * will be selected and followed up by  firing the {@link #rowselect} and     
     * {@link #selectionchange} events.     
     * @param {Number} row The index of the row to select    
     * @param {Boolean} keepExisting (optional) true to keep existing selections     
     * @param {Boolean} preventViewNotify (optional) Specify true to     
     * prevent notifying the view (disables updating the selected appearance)     
     * */    
    selectRow: function(index, keepExisting, preventViewNotify){
    	this.selModel.selectRow(index, keepExisting, preventViewNotify);
    } ,
    
    /**     
     * Returns true if there is a selection.     
     * @return {Boolean}     
     * */    
    hasSelection : function(){        
    	return this.selModel.hasSelection();    
    }
});
//para atajo
Extmx.GridPanel=Extmx.grid.GridPanel;
Ext.reg("gridmx",Extmx.GridPanel);